////////////////////////////////////////////////////////////
//	File:		"AL_Thread.h"
//	Author:		[AL] Anthony Lundquist
//	Created:	[AL] January 28, 2009
//	Purpose:	The Base thread Class
////////////////////////////////////////////////////////////
#ifndef AL_THREAD_H
#define AL_THREAD_H

#define WINDOWS_LEAN_AND_MEAN
#include <windows.h>
#include "SpinLock.h"

class AL_Thread
{
protected:
	HANDLE			m_hThreadHandle;
	unsigned int	m_dwThreadID;
	bool			m_bIsRunning;
	bool			m_bPaused;
	SpinLock		m_slLock;

	long			m_nExitCode;
public:
	AL_Thread();
	virtual ~AL_Thread();

	////////////////////////////////////////////////////////////
	//	Function:	Begin
	//	Created:	[AL] January 28, 2009
	//	Purpose:	Creates the thread
	//	In:			if we want to lock the calling thread
	//				until the new thread is running
	////////////////////////////////////////////////////////////
	void Begin(bool _bLockUntilInit = false);

	////////////////////////////////////////////////////////////
	//	Function:	End
	//	Created:	[AL] January 28, 2009
	//	Purpose:	Closes the thread
	////////////////////////////////////////////////////////////
	void End();

	////////////////////////////////////////////////////////////
	//	Function:	Pause
	//	Created:	[AL] January 28, 2009
	//	Purpose:	Pauses the thread
	////////////////////////////////////////////////////////////
	void Pause()	{	m_bPaused = true;	}

	////////////////////////////////////////////////////////////
	//	Function:	Resume
	//	Created:	[AL] January 28, 2009
	//	Purpose:	Unpauses the thread
	////////////////////////////////////////////////////////////
	void Resume()	{	m_bPaused = false;	}

	////////////////////////////////////////////////////////////
	//	Function:	Accessors
	//	Created:	[AL] January 28, 2009
	//	Purpose:	Returns a specific data member.
	//	Out:		The data member.
	////////////////////////////////////////////////////////////
	bool IsPaused()	{	return m_bPaused;	}
	bool IsRunning();
	DWORD GetExitCode(); // retrieves the return value of a thread after it exits

	////////////////////////////////////////////////////////////
	//	Function:	Mutators
	//	Created:	[AL] January 28, 2009
	//	Purpose:	Sets a specific data member.
	//	In:			The new value.
	////////////////////////////////////////////////////////////
	void SetIsRunning(bool _bRunning)	{	m_bIsRunning = _bRunning;	}

	////////////////////////////////////////////////////////////
	//	Function:	ThreadProc
	//	Created:	[AL] January 28, 2009
	//	Purpose:	the member func called for the thread
	//	Out:		the thread return value
	////////////////////////////////////////////////////////////
	DWORD ThreadProc();

	////////////////////////////////////////////////////////////
	//	Function:	ThreadMain
	//	Created:	[AL] January 28, 2009
	//	Purpose:	the main function for the new thread
	////////////////////////////////////////////////////////////
	virtual void  ThreadMain()= 0;

	////////////////////////////////////////////////////////////
	//	Function:	Init
	//	Created:	[AL] January 28, 2009
	//	Purpose:	functionality needed to be done on the new
	//				thread before main is called
	////////////////////////////////////////////////////////////
	virtual void Init() = 0;

	////////////////////////////////////////////////////////////
	//	Function:	Shutdown
	//	Created:	[AL] January 28, 2009
	//	Purpose:	functionality needed to be done on the thread
	//				before it is shutdown.
	////////////////////////////////////////////////////////////
	virtual void Shutdown() = 0;

};

#endif